﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MarioArena.Animation;
using Microsoft.Xna.Framework.Audio;

namespace MarioArena.World.Projectiles
{
    class Fireball : AbstractProjectile
    {
        public const float Speed = 500f; // Per second
        public const int FireDelay = 500;
        private SpriteAnimation mAnimation;
        private Texture2D dummyTexture;

        private long mSpawnTime;

        /// <summary>
        /// Constructs a new FireBall.
        /// </summary>
        /// <param name="id">The id of the fireball.</param>
        /// <param name="position">The spawnposition of the center of the fireball.</param>
        /// <param name="direction">The direction of the fireball.</param>
        public Fireball(long id, Vector2 position, Vector2 direction)
        {
            this.Id = id;
            this.SpawnPosition = position;
            this.Position = position;
            this.Direction = direction;
            this.Direction.Normalize();
            this.mSpawnTime = DateTime.Now.Ticks;

            int screenWidth = GameManager.Instance.HalfScreenWidth * 2;
            int screenHeight = GameManager.Instance.HalfScreenHeight * 2;
            // Create a rectangle containing the bounds on which the projectile gets deleted from the game.
            // The player should not be able to see the projectile disappearing.
            this.mDeletingBounds = new Rectangle(
                
                -screenWidth, // Half the screenwidth left of the outer left wall of the arena (the player is in the middle of the screen --> half the screenwidth TWICE)
                -screenHeight, // Half the screenheight above the outer top wall of the arena
                (TileEngine.Instance.LevelWidth * Tile.Width) + screenWidth, // Half the screenwidth to the right of the outer right wall of the arena
                (TileEngine.Instance.LevelHeight * Tile.Height) + screenHeight // Half the screenheight below the outer bottom wall of the arena
                );


            this.mDimensions = new Vector2(30, 30);
            
            this.mAnimation = new SpriteAnimation(GameManager.Instance.FireballSpritesheet, 8);
            this.mAnimation.framesPerSecond = 36;
            this.mAnimation.isLooping = true;

            // Flip the animation horizontally if the projectile is heading to the left
            if (this.Direction.X < 0) 
                this.mAnimation.spriteEffect = SpriteEffects.FlipHorizontally;


            dummyTexture = new Texture2D(GameManager.GraphicsDevice, 1, 1);
            dummyTexture.SetData(new Color[] { Color.White });
        }

        public override void Update(GameTime gameTime)
        {
            // Calculate the fireball's position depending on the spawn time. This way 
            // the server can use a parallel game loop (also depending on time elapsed since projectile spawn)
            // to calculate the fireball's position for collision
            float timeDifferenceInSeconds = (DateTime.Now.Ticks - this.mSpawnTime) / 10000000f;
            this.Position = this.SpawnPosition + ((Speed * Direction) * timeDifferenceInSeconds);

            this.mAnimation.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            this.mAnimation.Draw(spriteBatch,
                new Vector2(GameManager.Instance.HalfScreenWidth, GameManager.Instance.HalfScreenHeight)
                + this.Position - (this.mDimensions / 2)
                - new Vector2(Camera.X, Camera.Y)
                );
                
            int cameraX = Convert.ToInt32(this.Position.X);
            int cameraY = Convert.ToInt32(this.Position.Y);
        }
        
    }
}
